package com.ysd.lis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.TPermission;
import com.ysd.lis.entity.TPermissionClass;
import com.ysd.lis.entity.TRolePermission;
import com.ysd.lis.mapper.TPermissionClassMapper;
import com.ysd.lis.mapper.TPermissionMapper;
import com.ysd.lis.mapper.TRolePermissionMapper;
import com.ysd.lis.service.TPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * cold_chain.t_permission 服务实现类
 * </p>
 *
 * @author Peng
 * @since 2021-10-10
 */
@Service
public class TPermissionServiceImpl extends ServiceImpl<TPermissionMapper, TPermission> implements TPermissionService {
    @Autowired
    TPermissionMapper tPermissionMapper;

    @Autowired
    TPermissionClassMapper tPermissionClassMapper;
    @Autowired
    TRolePermissionMapper tRolePermissionMapper;

    @Override
    public Result findPermissionClassList() {
        List<Map<String,Object>> list = tPermissionMapper.findPermissionClassList();
        Map<String,Object> map = new HashMap<>();
        map.put("classList",list);
        return  Result.succ(1,"获取数据成功",map);
    }

    @Override
    public Result findPermissionListByClassId(String classId) {
        List<Map<String,Object>> list = tPermissionMapper.findPermissionListByClassId(classId);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("pmsList",list);
        return  Result.succ(1,"获取数据成功",resultMap);
    }

    @Override
    public Result addPermissionClass(TPermissionClass tPermissionClass) {
        int i = tPermissionClassMapper.insert(tPermissionClass);
        Map<String,Object> resultMap = new HashMap<>();
        if(i==1){
            resultMap.put("pmsClassId",tPermissionClass.getId());
            return  Result.succ(1,"添加成功",resultMap);
        }else{
            return Result.fail("添加失败");
        }

    }

    @Override
    public Result editPermissionClass(TPermissionClass tPermissionClass) {
        int i = tPermissionClassMapper.updateById(tPermissionClass);
        Map<String,Object> resultMap = new HashMap<>();
        if(i==1){
            resultMap.put("pmsClassId",tPermissionClass.getId());
            return  Result.succ(1,"修改成功",resultMap);
        }else{
            return Result.fail("修改失败");
        }
    }

    @Override
    public Result deletePermissionClass(TPermissionClass tPermissionClass) {
        int i = tPermissionClassMapper.updateById(tPermissionClass);
        Map<String,Object> resultMap = new HashMap<>();
        if(i==1){
            resultMap.put("pmsClassId",tPermissionClass.getId());
            return  Result.succ(1,"删除成功",resultMap);
        }else{
            return Result.fail("删除失败");
        }
    }

    @Override
    public Result addPermission(TPermission tPermission) {
        int i = this.baseMapper.insert(tPermission);
        Map<String,Object> resultMap = new HashMap<>();
        if(i==1){
            resultMap.put("pmsId",tPermission.getId());
            return  Result.succ(1,"添加成功",resultMap);
        }else{
            return Result.fail("添加失败");
        }
    }

    @Override
    public Result editPermission(TPermission tPermission) {
        int i = this.baseMapper.updateById(tPermission);
        Map<String,Object> resultMap = new HashMap<>();
        if(i==1){
            resultMap.put("pmsId",tPermission.getId());
            return  Result.succ(1,"修改成功",resultMap);
        }else{
            return Result.fail("修改失败");
        }
    }

    @Override
    public Result deletePermission(TPermission tPermission) {
        //删除此权限时先查看一下是否有角色存在此权限
        LambdaQueryWrapper<TRolePermission> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TRolePermission::getPermissionId,tPermission.getId()).eq(TRolePermission::getDelFlag,'0');
        Integer selectCount = tRolePermissionMapper.selectCount(lambdaQueryWrapper);
        if(selectCount>0){
            return Result.fail("有角色在使用此权限，删除失败");
        }else {
            int i = tPermissionMapper.deleteById(tPermission.getId());
            Map<String,Object> resultMap = new HashMap<>();
            if(i==1){
                resultMap.put("pmsId",tPermission.getId());
                return  Result.succ(1,"删除成功",resultMap);
            }else{
                return Result.fail("删除失败");
            }
        }
    }
}
